package database;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;

public class Insert extends JInternalFrame {
	Connection conn;
	DatabaseTest main;
	
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JTextField code;
	private JTextField ea;
	private JButton btnNewButton;
	private JLabel lblNewLabel_2;
	private JTextField serial;
	private JLabel label;
	
	
	/**
	 * Create the frame.
	 */
	public Insert(Connection c, DatabaseTest dt) {
		super("입력", true, true, true, false);
		setBounds(10, 10, 450, 175);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLblNewLabel_1());
		getContentPane().add(getLblNewLabel_2());
		getContentPane().add(getSerial());
		getContentPane().add(getCode());
		getContentPane().add(getEa());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getLabel());
		
		conn = c;
		this.main = dt;
	}
	public void addData() {
		try {
			String c = code.getText();
			int e = Integer.parseInt(ea.getText());
			
			String sql = "insert into ptbl(serial, code, ea) values(ptbl_serial.nextval, ?, ?)";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, c);
			pstmt.setInt(2, e);
			
			pstmt.executeUpdate();
			
//			if
			
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("serial");
			lblNewLabel.setBounds(12, 10, 31, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("code");
			lblNewLabel_1.setBounds(12, 35, 31, 15);
		}
		return lblNewLabel_1;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setBounds(55, 32, 367, 21);
			code.setColumns(10);
		}
		return code;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setBounds(55, 57, 367, 21);
			ea.setColumns(10);
		}
		return ea;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("저장");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try {
						String c = code.getText();
						int e = Integer.parseInt(ea.getText());
						
						String sql = "insert into ptbl(serial, code, ea) values(ptbl_serial.nextval, ?, ?)";
						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setString(1, c);
						pstmt.setInt(2, e);
						
						pstmt.executeUpdate();
						
					} catch(Exception e) {
						e.printStackTrace();
					}
				}
			});
			btnNewButton.setBounds(55, 88, 367, 23);
		}
		return btnNewButton;
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("ea");
			lblNewLabel_2.setBounds(12, 60, 31, 15);
		}
		return lblNewLabel_2;
	}
	private JTextField getSerial() {
		if (serial == null) {
			serial = new JTextField();
			serial.setBounds(55, 7, 367, 21);
			serial.setColumns(10);
		}
		return serial;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel(" ");
			label.setBounds(12, 121, 410, 15);
		}
		return label;
	}
}
